// pages/zpsc/itoapply/itoapply.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityId: null,
    fields: [], //单位自定义表单字段
    checkUserIDCardStatus: 0,//身份证是否必填
    checkUserMobileStatus: 0,//手机号是否必填
    checkUserNameStatus: 0,//用户姓名是否必填
    checkAreaStatus: 0,//区域归属是否必填
    addressInfoList: [], //区域归属列表
    defaultAddress: "", //默认
    areaId: '',//区域归属ID
    cover: '', //封面
    uploadStatus: '' //是否上传作品
  },
  // 提交表单
  formSubmit:function(e){
    var that = this;
    console.log(e);
    var paras = ''; //拼接参数
    //判断固定数据数据
    if(that.data.checkUserNameStatus == 1 && e.detail.value.userName.length == 0){
      wx.showToast({
        title: '姓名不能为空！',
        icon: 'none',
        duration: 2000
      })
      return false;
    }else if(that.data.checkUserMobileStatus == 1 && e.detail.value.userMobile.length == 0){
      wx.showToast({
        title: '手机号不能为空！',
        icon: 'none',
        duration: 2000
      })
      return false;
    }else if(that.data.checkUserMobileStatus == 1 && e.detail.value.userMobile.length != 11){
        wx.showToast({
          title: '手机号格式错误！',
          icon: 'none',
          duration: 2000
        })
        return false;
    }else if(that.data.checkUserIDCardStatus == 1 && e.detail.value.IDCard.length == 0){
      wx.showToast({
        title: '身份证号码不能为空！',
        icon: 'none',
        duration: 2000
      })
      return false;
    }else if(that.data.checkUserIDCardStatus == 1 && e.detail.value.IDCard.length != 18){
      wx.showToast({
        title: '身份证号码格式不正确！',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    var userName = e.detail.value.userName == undefined ? '' : e.detail.value.userName;
    var userMobile = e.detail.value.userMobile == undefined ? '' : e.detail.value.userMobile;
    var IDCard = e.detail.value.IDCard == undefined ? '' : e.detail.value.IDCard;
    paras += "?userName=" + userName + "&userMobile=" + userMobile + "&fid=" + app.globalData.fid + "&uid=" + wx.getStorageSync('cxid') + "&activityId=" + that.data.activityId + '&IDCard=' + IDCard + "&addressInfoId=" + that.data.areaId;
    //判断是否有自定义数据 如果有的话，判断是否为空
    if(that.data.fields.length > 0){
      var fields = that.data.fields;
      for(var i = 0;i < fields.length;i++){
        var name = fields[i].id;
        console.log(e.detail.value[name]);
        if(e.detail.value[name].length == 0){
          wx.showToast({
            title: fields[i].addKey+ '不能为空！',
            icon: 'none',
            duration: 2000
          })
          return false;
        }else{
          paras += "&_addKey_=" + e.detail.value[name] + '_val_' + fields[i].id;
        }
      }
    }
    console.log("拼接后参数： " + paras);
    var userinfo = wx.getStorageSync('userInfo');
    console.log(userinfo.nickName);
    wx.request({
      url: app.globalData.activityHost + '/services/activityaction/joinActivity' + paras,
      success: function (res) {
        console.log(res);
        if (res.data.result == "0") {
          wx.showModal({
            title: '报名成功',
            content: '点击确定按钮完成',
            showCancel: false,
            success: (res) => {
              if (res.confirm) {
                if(that.data.uploadStatus == 1){
                  wx.redirectTo({
                    url: '/pages/zpsc/upload/upload?activityId=' + that.data.activityId  + "&cover=" + that.data.cover,
                  })
                }else{
                  wx.navigateBack({
                    
                  })
                }
              }
            }
          })
        } else {
          wx.showToast({
            title: '报名失败，请稍后再试',
            icon: 'none'
          })
        } 
      },
      fail: function () {
       wx.showToast({
         title: '报名失败，请稍后再试',
         icon: 'none'
       })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //屏蔽右上角分享功能
    wx.hideShareMenu();
    //初始化活动ID
    var addressInfos = JSON.parse(options.addressInfoList);
    var defaultAddress = "";
    var areaId = "";
    if(addressInfos.length > 0){
      defaultAddress = {id: addressInfos[0].id, name: addressInfos[0].cName};
      areaId = addressInfos[0].id;
    }
    this.setData({
      activityId: options.activityId,
      checkUserIDCardStatus: options.checkUserIDCardStatus,
      checkUserNameStatus: options.checkUserNameStatus,
      checkUserMobileStatus: options.checkUserMobileStatus,
      checkAreaStatus: options.checkAreaStatus,
      addressInfoList: addressInfos,
      defaultAddress: defaultAddress,
      areaId: areaId,
      cover: options.cover,
      uploadStatus: options.uploadStatus
    })
    this.getFields(options.activityId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //获取活动自定义字段
  getFields: function(id){
    var that = this;
    wx.request({
      url: app.globalData.activityHost + '/services/activityaction/getAddUserInfoByActivity?id=' + that.data.activityId,
      success: function (res) {
        console.log(res);
        if (res.data.result == "0") {
          that.setData({
            fields: res.data.obj
          })
        } 
      },
      fail: function () {
       wx.showToast({
         title: '获取自定义字段失败，请稍后再试',
         icon: 'none'
       })
      }
    })
  },

  //监听select框
  selectchange: function(e){
    this.setData({
      areaId: e.detail.id
    })
  }
})